package com.liyuan.help_e.controller;

import com.liyuan.help_e.model.News;
import com.liyuan.help_e.model.Page;
import com.liyuan.help_e.model.Question;
import com.liyuan.help_e.model.ResultJson;
import com.liyuan.help_e.service.NewsService;
import com.liyuan.help_e.service.PageService;
import com.liyuan.help_e.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
public class IndexController {
    @Value("${hotTopic.size}")
    private Integer hotTopicsSize;
    @Autowired
    QuestionService questionService;
    @Autowired
    PageService pageService;
    @Autowired
    NewsService newsService;


    @PostMapping("/news/{page}/{size}")
    public ResultJson listNews(@PathVariable("page")Integer page,
                               @PathVariable("size")Integer size){
        News[] news = newsService.selectNewsByPage(page,size);
        return new ResultJson(200,"get news success!",news);
    }


    @GetMapping("/questions/{type}/{page}/{size}")
    public ResultJson getQuestionsByPage(@PathVariable("page") Integer page,@PathVariable("size")Integer size,@PathVariable("type")String type) {
        Question[] questions=null;
        if(type.equals("questions")){
            questions = questionService.selectQuestionsByPage(page, size);
        }else if(type.equals("study")||type.equals("life")||type.equals("resource")){
            questions = questionService.selectQuestionsByPageAndTag(page,size,type);
        }
        return new ResultJson(200, "get questions_index success!", questions);
    }

    @GetMapping("/hotTopics/{type}/{size}")
    public ResultJson getHotTopics(@PathVariable("type")String type,
                                   @PathVariable("size")Integer size) {
        if(type.equals("questions")||type.equals("all")){
            Question[] questions = questionService.selectQuestionsByReadCount(size);
            return new ResultJson(200, "get hotTopic success!", questions);
        }else if(type.equals("news")){
            News[] news = newsService.selectNewsByReadCount(size);
            return new ResultJson(200, "get hotTopic success!", news);
        }else if(type.equals("life")||type.equals("study")||type.equals("resource")){
            Question[] questions = questionService.selectQuestionsByReadCountAndTag(size,type);
            return new ResultJson(200, "get hotTopic success!", questions);
        }
        return null;

    }

    @GetMapping("/page/{type}/{page}/{size}")
    public ResultJson getPagination(@PathVariable("type") String type, @PathVariable("page") Integer page, @PathVariable("size")Integer size,@RequestParam(required = false) String keywords) {
        //目前共三种分页，所有问题；按标签分类的所有问题；按搜索的问题
        Page pageObject = null;
        if (type.equals("questions")) {
            pageObject = pageService.paging(page,size,questionService.sumAllQuestions());
        }else if(type.equals("study")||type.equals("life")||type.equals("resource")){
            pageObject = pageService.paging(page,size,questionService.sumQuestionsByTag(type));
        }else if(type.equals("news")){
            pageObject = pageService.paging(page,size,newsService.sumAllNews());
        }
        return new ResultJson(200,"get pagination success!",pageObject);
    }

    @GetMapping({"/all","/study","/life","/resource","news"})
    public void toList_Html(HttpServletRequest request,
                            HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/list.html").forward(request,response);
    }

    @GetMapping("/carousel/{number}")
    public ResultJson carousel(@PathVariable("number")Integer number){
        News[] news = newsService.selectNewsByScale(number);
        return new ResultJson(200,"get carousel success!",news);
    }
}
